<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: Imagebox.php 134 2009-05-24 09:48:43Z movoin $
 */

function _ctlImagebox($name, $args=null)
{
    $opts = array('id', 'url', 'limit', 'width', 'height', 'site');
    $data = FLEA_WebControls::extractAttribs($args, $opts);
    FLEA_WebControls::mergeAttribs($args);

    if(!$data['site'])
        $data['site'] = 0;
    if(!$data['limit'])
        $data['limit'] = 5;
    if(!$data['width'])
        $data['width'] = 380;
    if(!$data['height'])
        $data['height'] = 240;

    //缓存过期时间
    $deploy = defined('DEPLOY_MODE') && DEPLOY_MODE;
    if ( $deploy ) {
        $lifetime = 86400; //一天
    } else {
        $lifetime = 0;
    }
    //缓存名称
    $cache_id = 'index.imagebox.' . $name . '.' . $data['site'];
    if($data['id'])
        $cache_id .= '.' . $data['id'];
    //从缓存中读取数据
    $content = FLEA::getCache($cache_id, $lifetime, true, true);
    if(!is_array($content)) {
        //载入内容表
        $tblContent =& FLEA::getSingleton( 'Table_Contents' );
        $content = $tblContent->find((int)$data['id'], null, 'id,content');
        FLEA::writeCache( $cache_id, $content, true );
    }
    $body = $content['content'];
    if($body) {
        preg_match_all("/(src|SRC)=[\"|'| ]{0,}(\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU", $body, $img_array);
        if($img_array[2]) {
            for($i=0;$i<$data['limit'];$i++) {
                if($img_array[2][$i]) {
                    $imgs[] = $img_array[2][$i];
                } else {
                    break;
                }
            }
            $files = implode('|', $imgs);
        }
        $output = '<script type="text/javascript">';
        $output .= 'var swf_width=' . $data['width'] . ';';
        $output .= 'var swf_height=' . $data['height'] . ';';
        $output .= 'var files=\'' . $files . '\';';
        $output .= 'var links=\'' . $data['url'] . '\';';
        $output .= 'document.write(\'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="\'+ swf_width +\'" height="\'+ swf_height +\'">\');';
        $output .= 'document.write(\'<param name="movie" value="/site/modules/Imagebox/bcastr3.swf"><param name="quality" value="high">\');';
        $output .= 'document.write(\'<param name="menu" value="false">\');';
        $output .= 'document.write(\'<param name="FlashVars" value="bcastr_file=\'+files+\'&bcastr_link=\'+links+\'">\');';
        $output .= 'document.write(\'<embed src="/site/modules/Imagebox/bcastr3.swf" FlashVars="bcastr_file=\'+files+\'&bcastr_link=\'+links+\'& menu="false" quality="high" width="\'+ swf_width +\'" height="\'+ swf_height +\'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\');';
        $output .= 'document.write(\'</object>\');';
        $output .= '</script>';
    }
    return $output;

}
